<?php

/**
	参数说明
**/
$accessKey = "I1I7CLU4708LI8XQ0ODL"; // 用户秘钥对：开通快云存储时的Access_Key，可在会员中心获取
$secretKey = "N41hrYy14gI03RpnZijfezGl/VUtZP3b77vjF6nn"; // 用户秘钥对：开通快云存储时的Secret_Key，可在会员中心获取
$resource = "VG3OQ81wtrNGANeuq8IdwWxGFK0xdA3X"; // 调用来源，联系快云存储协作获取
$voucher = "dcb99d196dd2418ec1fdfa7269ee4b81"; // 用户通过accesskey和secretkey获取的，可在会员中心点击，以邮件形式获取
//
$bucketName = "demobkt"; // 用户的空间名称

$token = "" ; // 用户操作秘钥，是用户调用api接口时必须用到的的秘钥；可以通过API获取

/**
 * 上传变量
 */
$localFile = "C:\\demo.png";// 本地图片路径
$fileName = "test/demo.png"; //上传到快云存储中显示的文件名；如果是多级的，自动创建文件夹，例test/demo.png，自动创建test目录，并在该目录下上传demo.png文件

/**
 * 获取文件访问链接变量
 */
$keyName = "test/demo.png"; // 文件完整路径，例test/demo.png
$minutes = 0; // 获取链接时效；默认是0，即无时效，永久有效。
$leng = 0; // 是否获取长链接。若链接时效不为0，即为长链接。链接时效为0，默认是leng = 0，即短链接；leng = 1, 是长链接。
	
/**
 * 删除文件变量
 */
$delfileName = "test/demo.png";



class URLRequest
{
    public $url;
    public $headers;
    public $params;
    public $body;
    public $expectedFormat;
    public $method;
	public $data;


	/**
		公共方法
	**/
    public function URLRequest($aUrl, array $aHeaders, array $aParams, $aFormat = "json", $isPost = false, $aBody = "+")
    {
        $this->url = $aUrl;
        $this->headers = $aHeaders;
        $this->params = $aParams;
        $this->expectedFormat = $aFormat;
        $this->method = ($isPost ? "POST" : "GET");
        $this->body = $aBody;

    }
	//
    public function exec()
    {

        $url = $this->url;

        $request = curl_init();
        curl_setopt($request, CURLOPT_URL, $url);
        curl_setopt($request, CURLOPT_HEADER, 1);
        curl_setopt($request, CURLOPT_HTTPHEADER, $this->headers);
        curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
        if($this->method == "POST")
        {
            curl_setopt($request, CURLOPT_POST, 1);
            curl_setopt($request, CURLOPT_POSTFIELDS, $this->body);
        }

        $response = curl_exec($request);
		$httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE);
		if ($httpCode == '200') {
			$headerSize = curl_getinfo($request, CURLINFO_HEADER_SIZE);
			$header = substr($response, 0, $headerSize);
			$body = substr($response, $headerSize);
			curl_close($request);
			return $body;
		}else{
			curl_close($request);
			return $response;
		}
    }
}

/** 获取操作秘钥token函数 **/
function get_token($voucher,$accessKey,$secretKey,$resource){
	$url = "http://api.storagesdk.com/restful/storageapi/storage/getToken"; //获取操作秘钥token方法
	$query = array();
	$data = array();
	$data["voucher"]= $voucher;
	$data["accessKey"]= $accessKey ;
	$data["secretKey"]= $secretKey;
	$data["resource"]= $resource;
	$body = json_encode($data);
	$headers = array("Content-Type: application/json; charset=utf-8");
	$request = new URLRequest($url, $headers, $query, "json", true, $body);
	$response = $request->exec();
	$msg = json_decode($response,true)["message"];
	$arr = explode(":",$msg);
	$token = $arr[1];
	return $token;
	//print_r($response);
}

/** 上传文件函数 **/
function send_file($localFile,$fileName,$token,$bucketName,$resource){
	$url = "http://api.storagesdk.com/restful/storageapi/file/uploadFile"; //上传文件方法
	$query = array();
	$data = array();
	$data['input'] = file_get_contents($localFile);
	$file = base64_encode($fileName);
	$len = strlen(file_get_contents($localFile));
	//print($len);
	//print("<br/>");
	$headers = array("Content-Type: application/json;charset=utf-8",
		"token:{$token}",
		"fileName:{$file}",
		"bucketName:{$bucketName}",
		"resource:{$resource}",
		"length:{$len}");
	$body = $data["input"];
	$request = new URLRequest($url, $headers, $data, "json", true, $body);
	$response = $request->exec();
	$msg = json_decode($response,true)["message"];
	return $msg;
	//print_r($response);
}

/** 获取文件的url函数 **/
function get_url($token,$fileName,$bucketName,$minutes,$leng,$resource){
	$url = "http://api.storagesdk.com/restful/storageapi/file/getFileUrl"; // 获取文件的url方法
	$query = array();
	$data = array();
	$data["token"]= $token;
	$data["fileName"]= $fileName;
	$data["bucketName"]= $bucketName;
	$data["minutes"]= $minutes;
	$data["leng"]= $leng;
	$data["resource"]="{$resource}";
	$body = json_encode($data);
	//print_r($body);
	$headers = array("Content-Type: application/json; charset=utf-8");
	$request = new URLRequest($url, $headers, $query, "json", true, $body);
	$response = $request->exec();
	$url = json_decode($response,true)["message"];
	return $url;
	//print_r($response);
}

/** 删除文件函数**/
function del_file($token,$fileName,$bucketName,$resource){
	$url = "http://api.storagesdk.com/restful/storageapi/file/deleteFile"; // 删除文件方法
	$query = array();
	$data = array();
	$data["token"] = $token;
	$data["fileName"] = $fileName;
	$data["bucketName"] = $bucketName;
	$data["resource"] = $resource;
	$body = json_encode($data);
	$headers = array("Content-Type: application/json; charset=utf-8");
	$request = new URLRequest($url, $headers, $query, "json", true, $body);
	$response = $request->exec();
	$result	= json_decode($response, true)["message"];
	return $result;
	
}

/*获取操作秘钥token*/
$token = get_token($voucher,$accessKey,$secretKey,$resource);
printf("获取操作秘钥token：%s", $token);print("<br/>");
/*上传文件*/
$request = send_file($localFile,$fileName,$token,$bucketName,$resource);	
printf("上传文件%s", $request);print("<br/>");
/*获取文件的url*/
$fileUrl = get_url($token,$keyName,$bucketName,$minutes,$leng,$resource);
printf("获取文件的url：%s", $fileUrl);print("<br/>");
/*删除文件*/
$resultIn = del_file($token,$fileName,$bucketName,$resource);
printf("删除文件%s", $resultIn);
?>